<?php 
namespace app\api\controller;

use app\api\ApiController;
use common\libs\BlockChain;
use common\model\sell66\BotInv;
use common\service\ChainService;
use common\service\TradeOfferService;
use common\service\AuthService;
use common\libs\AES;
use common\service\InvService;
use think\Config;

class App extends ApiController{
	public function demo()
    {
//        $res = ChainService::getInstance()->getIssueId(38);
//        $res = ChainService::getInstance()->checkIssueStatus(10);
//        $res = ChainService::getInstance()->takeBack(9, 50);
        $res = ChainService::getInstance()->checkTakeBackStatus(50);
//        $res = ChainService::getInstance()->assetList(2);
dump($res);die;
        $arr = [
            'issue_id' => 12,
            'status' => 2,
            'asset' => 'I0ZqE7HZmV1iYG5XvNKdv+UlEU62N6pwH8QbCcCRtNg=',
        ];
        $config = Config::get('blockwallet_config');
        $blackChain = new BlockChain();
        $blackChain->setPrivateKey($config['private_key']);
        $blackChain->setBot($config['bot']);
        $blackChain->setRequestUrl($config['request_url']);
        $blackChain->setCallBackWallet($config['callback_wallet']);
        $blackChain->setCallBackStatus($config['callback_status']);
        $sign = $blackChain->getSign($arr);
        $arr['sign'] = $sign;
        $json = json_encode($arr);
        $data = base64_encode($json);
        dump($data);die;
    }
	/**
	 * @api {get} /app/games 获取游戏代码
	 * @apiVersion 1.0.0
	 * @apiName games
	 * @apiGroup app
	 *
	 * @apiDescription  获取游戏代码
	 *
	 *
	 * @apiError 20000 请求参数错误
	 *
	 * @apiErrorExample Response (example):
	 *  {
	 *		code: 99999,
	 *		msg: "请求成功",
	 *		data: [
	 *			{
	 *			appId : 730,
	 *			name : "csgo"
	 *			},
	 *		]
	 *	}
	 */
	public function games(){
		$games = InvService::getInstance()->appIds;
		$res = [];
		foreach ($games as $game){
			$res[] = $game;
		}
		return $this->successJson($res);
	}
	
	/**
	 * @api {get} /app/banks  银行列表
	 * @apiVersion 1.0.0
	 * @apiName banks
	 * @apiGroup app
	 *
	 * @apiDescription  银行列表
	 *
	 *
	 * @apiError 20000 请求参数错误
	 *
	 * @apiExample Response (example):
	 *  {
	 *	code: 99999,
	 *	msg: "请求成功",
	 *	data: [
	 *			{
	 *			id: 1,
	 *			bankCode: "ZSYH",
	 *			bankName: "招商银行",
	 *			bankIcon: null
	 *			}
	 *		]
	 *	}
	 */
	public function banks(){
		$banks = AppService::getInstance()->getBanks();
		return $this->successJson($banks);
	}
	
	/**
	 * @api {get} /app/nearRecordOrder 最近成交记录
	 * @apiVersion 1.0.0
	 * @apiName nearRecordOrder
	 * @apiGroup app
	 *
	 * @apiDescription    最近成交记录
	 *
	 * @apiError 20000  请求参数错误
	 *
	 * @apiSuccessExample Response (example):
	 * {
	 *	 code: 99999,
	 *	 msg: "请求成功",
	 *	 data: {
	 *		 countOrder: 0,
	 *		 orders: [
	 *			 {
	 *			 userName:"卖火柴的小女孩",
	 *			 price : 89.00,
	 *			 finishedTime: "2015-11-11 10:00:00"
	 *			 }
	 *		 ]
	 *		}
	 *	 }
	 */
	public function nearRecordOrder(){
		$result = TradeOfferService::getInstance()->nearRecordInfo();
		return $this->successJson($result);
	}
	
	//验证码
	public function checkVerify(){
		$user = AuthService::getInstance()->verifyToken();
        $userId = 0;
		if(!empty($user)){
            $userId = $user['user_id'];
		}
		return CaptchaService::getInstance()->create($userId,'feedback');
	}
	
	public function validateVerify($code){
		$user = AuthService::getInstance()->verifyToken();
		if(empty($user)){
			return $this->errorMessage(BAD_ACCESS_TOKEN_NO,BAD_ACCESS_TOKEN_ERROR);
		}
		$userId = $user['user_id'];
		$result =  CaptchaService::getInstance()->checkVerify($userId, $code);
		return $this->successJson($result);
	}
	
	public function steamAuthUrl($invite_token=""){
		return $this->successJson(array('url'=>getenv('steam_login_page').'?invite_token='.$invite_token));
	}
	
	public function redirectUrl($invite_token){
		$aes = new AES();
		$str = $aes->decode($invite_token);
		$strArr = explode('|', $str);
		if(empty($strArr[1])){
			$url = '/#/site/index';
		}elseif($strArr[0] == 'commission'){
			$url = '/#/site/activity';
		}elseif($strArr[0] == 'duanwu'){
			$url = '/#/site/treasure';
		}elseif($strArr[0] == 'raffle'){
			$url = '/#/site/treasure';
		}elseif($strArr[0] == 'treasure'){
            $url = '/#/site/treasure';
        }elseif($strArr[0] == 'losegems'){
        	$url = '/#/site/treasure';
        }elseif($strArr[0] == 'action'){
        	$url = '/#/site/activity';
        }elseif($strArr[0] == 'TI7'){
        	$url = '/#/site/ti7';
        }
		return $this->successJson($url);
	}
}